package com.platform.qujia.config;

import com.platform.qujia.enums.WxConsumptionMessageEnum;
import com.platform.qujia.pojo.dto.UniversalRedisMessageDto;
import com.platform.qujia.service.RedisConsumptionThemeService;
import com.platform.qujia.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Slf4j
@Component
public class RedisMessageListener implements MessageListener {

	@Autowired
	private RedisConsumptionThemeService  redisConsumptionThemeService;
	
	@Override
	public void onMessage(Message message, byte[] pattern) {
	    String body = new String(message.getBody());
	    String themeStr = new String(pattern);
	    log.info("---------开始消费----------主题："+themeStr);
	    UniversalRedisMessageDto red=null;
	    try {
			red = JsonUtils.jsonToObject(body, UniversalRedisMessageDto.class);
		} catch (IOException e) {
			log.error("消息转换失败,body:[{}]",body);
			log.error(e.getMessage(), e);
			return;
		}
	    log.info("消费者-----接收的标题:"+red.getTitle());
		if(RedisConfig.TOPIC_Template_message_push_1.equals(themeStr)){
			redisConsumptionThemeService.couponMessageSubscription(red.getData(),WxConsumptionMessageEnum.getByValue(red.getMessageType()));
		}
		if(RedisConfig.TOPIC_REDUCTION_ACTIVITY.equals(themeStr)){
			//参与充值立减活动
			redisConsumptionThemeService.reductionActivityMessageSubscription(red.getData(),WxConsumptionMessageEnum.getByValue(red.getMessageType()));
		}

	}
}